﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.Clustering
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class FeatureSetOutput:ResultBase
    {
        #region props
        private int _IterationCount;
        /// <summary></summary>
        public int IterationCount
        {
            get
            {
                return this._IterationCount;
            }
            set
            {
                this._IterationCount = value;
            }
        }

        private double _FinalOverallPerformanceScore;
        /// <summary></summary>
        public double FinalOverallPerformanceScore
        {
            get
            {
                return this._FinalOverallPerformanceScore;
            }
            set
            {
                this._FinalOverallPerformanceScore = value;
            }
        }

        private double _AvgTermWeight;
        /// <summary></summary>
        public double AvgTermWeight
        {
            get
            {
                return this._AvgTermWeight;
            }
            set
            {
                this._AvgTermWeight = value;
            }
        }

        private double _MinTermWeight;
        /// <summary></summary>
        public double MinTermWeight
        {
            get
            {
                return this._MinTermWeight;
            }
            set
            {
                this._MinTermWeight = value;
            }
        }

        private double _MaxTermWeight;
        /// <summary></summary>
        public double MaxTermWeight
        {
            get
            {
                return this._MaxTermWeight;
            }
            set
            {
                this._MaxTermWeight = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public FeatureSetOutput():base(Guid.Empty,Guid.Empty)
        {
            this._IterationCount = 0;
            this._FinalOverallPerformanceScore = 0;
            this._AvgTermWeight = 1;
            this._MinTermWeight = 1;
            this._MaxTermWeight = 1;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="iterationCount"></param>
        /// <param name="finalPerformance"></param>
        /// <param name="avgTermWeight"></param>
        /// <param name="minTermWeight"></param>
        /// <param name="maxTermWeight"></param>
        public FeatureSetOutput(Guid componentID, Guid jobTicketID,
            int iterationCount,
            double finalPerformance,
            double avgTermWeight,
            double minTermWeight,
            double maxTermWeight) 
            : base(componentID, jobTicketID)
        {
            this._IterationCount = iterationCount;
            this._FinalOverallPerformanceScore = finalPerformance;
            this._AvgTermWeight = avgTermWeight;
            this._MinTermWeight = minTermWeight;
            this._MaxTermWeight = maxTermWeight;
        }
        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xNode"></param>
        /// <returns></returns>
        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FeatureSetOutput output = new FeatureSetOutput();
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.IterationCount = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "IterationCount", "0"));
                output.FinalOverallPerformanceScore = double.Parse(
                    XmlDataUtil.GetAttributeValue(xNode, "FinalOverallPerformanceScore", "0"));
                output.AvgTermWeight = double.Parse(
                    XmlDataUtil.GetAttributeValue(xNode, "AvgTermWeight", "1"));
                output.MinTermWeight = double.Parse(
                    XmlDataUtil.GetAttributeValue(xNode, "MinTermWeight", "1"));
                output.MaxTermWeight = double.Parse(
                    XmlDataUtil.GetAttributeValue(xNode, "MaxTermWeight", "1"));
                
                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IterationCount", this.IterationCount.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinalOverallPerformanceScore",
                                        this.FinalOverallPerformanceScore.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"AvgTermWeight",this.AvgTermWeight.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc,xNode,"MinTermWeight",this.MinTermWeight.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"MaxTermWeight",this.MaxTermWeight.ToString());

            return xNode;
        }
    }
}
